package com.ctg.itrdc.imageconsole.common.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.ProtocolException;
import java.net.URL;

import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;

/**
 * 
 * @description: HTTP请求工具类
 * @version: v1.0.0 
 * @author: Piao.Wu
 * 
 * Modification History:
 * Date               Author      Version     Description
 * -----------------------------------------------------------------
 * 2016年8月18日 下午7:49:46     Piao.Wu       v1.0.0        create
 *
 */
public class HttpRequestUtil {

    public ResteasyClient client;
    public static WebTarget target;

    public HttpRequestUtil(URL url) {
        client = new ResteasyClientBuilder().build();
        target = client.target(url.toString());
    }

    public String getResponseJsonData(Response response) throws IOException, ProtocolException, UnsupportedEncodingException {
        try {
            if (response.getStatus() >= 300) {
            	/*if (response.getStatus() == 500) {
            		return response.readEntity(String.class);
				}*/
                throw new RuntimeException("Failed with HTTP error code : " + response.getStatus() + " error message is : " + response.readEntity(String.class));
            }
            return response.readEntity(String.class);
        } finally {
            response.close();
            client.close();
        }
    }
    
    public boolean isConnectSuccess(Response response) throws IOException, ProtocolException, UnsupportedEncodingException {
        try {
            if (response.getStatus() < 300) {
                return true;
            }
            return false;
        } finally {
            response.close();
            client.close();
        }
    }
}
